package cn.edu.xmu.oomall.order.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.order.mapper.OrderPoMapper;
import cn.edu.xmu.oomall.order.mapper.PaymentPoMapper;
import cn.edu.xmu.oomall.order.model.bo.Order;
import cn.edu.xmu.oomall.order.model.po.OrderPo;
import cn.edu.xmu.oomall.order.model.po.OrderPoExample;
import cn.edu.xmu.oomall.order.model.po.PaymentPo;
import cn.edu.xmu.oomall.order.model.po.PaymentPoExample;
import cn.edu.xmu.oomall.order.model.vo.OrderPaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

@Repository
public class OrderDao {
    @Autowired
    private OrderPoMapper orderPoMapper;
    @Autowired
    private PaymentPoMapper paymentPoMapper;


    /**
     * 查询自己订单的支付信息
     * @param paymentPoExample
     * @return ReturnObject
     */
    public ReturnObject getOrderPayments(PaymentPoExample paymentPoExample) {
        try {
            List<PaymentPo> paymentPos = paymentPoMapper.selectByExample(paymentPoExample);
            List<OrderPaymentVo> orderPaymentVos = new ArrayList<>();
            for(PaymentPo paymentPo:paymentPos) {
                orderPaymentVos.add(cloneVo(paymentPo, OrderPaymentVo.class));
            }
            return new ReturnObject(orderPaymentVos);
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 获取订单所有状态
     * @return
     */
    public ReturnObject getOrderState(){
        List<Map<String,Object>> stateList=new ArrayList<>();
        for(Order.State state:Order.State.values()){
            Map<String,Object> temp=new HashMap<>();
            temp.put("code",state.getCode());
            temp.put("name",state.getDescription());
            stateList.add(temp);
        }
        return new ReturnObject<>(stateList);
    }

    /**
     * 增加订单
     */
    public ReturnObject insertOrder(Order order){
        OrderPo orderPo= cloneVo(order,OrderPo.class);
        try{
            int ret=orderPoMapper.insert(orderPo);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(orderPo);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }


    /**
     * 修改订单
     */
    public ReturnObject updateOrder(Order order){
        OrderPo orderPo= cloneVo(order,OrderPo.class);
        try{
            int ret=orderPoMapper.updateByPrimaryKey(orderPo);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }


    /**
     * 根据条件查看订单
     */
    public ReturnObject<List> selectOrders(OrderPoExample example){
        try{
            List<OrderPo> orderPos=orderPoMapper.selectByExample(example);
            List<Order> orders=new ArrayList<>(orderPos.size());
            for(OrderPo orderPo:orderPos){
                Order order=cloneVo(orderPo,Order.class);
                orders.add(order);
            }
            return new ReturnObject<>(orders);
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据父订单id查看子订单
     */
    public ReturnObject<List> selectChildOrders(Long pId){
        OrderPoExample example=new OrderPoExample();
        OrderPoExample.Criteria criteria=example.createCriteria();
        criteria.andPidEqualTo(pId);
        try{
            List<OrderPo> orderPos=orderPoMapper.selectByExample(example);
            List<Order> orders=new ArrayList<>(orderPos.size());
            for(OrderPo orderPo:orderPos){
                orders.add(cloneVo(orderPo,Order.class));
            }
            return new ReturnObject<>(orders);
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据id查看订单
     */
    public ReturnObject selectOrderById(Long id){
        try{
            OrderPo orderPo=orderPoMapper.selectByPrimaryKey(id);
            Order order=cloneVo(orderPo,Order.class);
            return new ReturnObject(order);
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

}
